import { _decorator, Component, Label, Node } from 'cc';
import { MapZone } from '../entity/MapZone';
import { UserInfo } from '../common/UserInfo';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { ArmyIndex } from '../army/ArmyIndex';
import { BattleLayout } from '../army/BattleLayout';
import { Village } from '../entity/Village';
const { ccclass, property } = _decorator;

@ccclass('PanelOasis')
export class PanelOasis extends Component {
    @property(Label)
    public natural: Label//绿洲资源
    @property(Label)
    public level: Label // 绿洲等级
    @property(Label)
    public owner: Label // 绿洲归属
    @property(Node)
    public detection:Node

    private zone:MapZone
    private villageData:Village
    start() {
        this.detection.on(Node.EventType.TOUCH_END, this.onDetection, this)
    }

    setData(zone:MapZone, data:Village){
        this.zone = zone
        this.villageData = data

        let obj: any = MapZone.zoneTypeToNum(zone.typeId)

        let str = ''
        for (let key in obj) {
            switch (key) {
                case 'f':
                    str += `粮+${obj[key]}%,`
                    break
                case 'i':
                    str += `铁+${obj[key]}%,`
                    break
                case 'm':
                    str += `石+${obj[key]}%,`
                    break
                case 'w':
                    str += `木+${obj[key]}%、`
                    break
            }
        }
        str = str.substring(0, str.length - 1)

        this.natural.string = str
        // TODO 没有返回绿洲等级
        this.level.string = '顶级,怪物增长的很快'


        let xId = UserInfo.instance.villageData.xId
        let yId = UserInfo.instance.villageData.yId
        if(Math.abs(zone.x - xId) <=3 && Math.abs(zone.y - yId) <=3){
            this.owner.string = '绿洲在你当前村庄的可占领范围内'
        } else {
            this.owner.string = '绿洲不在你当前村庄的可占领范围内'
        }
    }

    async onDetection(){
        let ui = await UIManager.instance.openUI(PrefabEnum.ArmyIndex)
        let armyIndex = ui.getComponent(ArmyIndex)
        armyIndex.onBattle()
        let bl = armyIndex.content.children[0].getComponent(BattleLayout)
        bl.onTargetVillage(this.villageData)
        
        this.scheduleOnce(() => {
            bl.onLook()
        }, 0.5)
        
    }
}


